package newx.com.control.Scroll
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	
	import newx.com.NxUIComponent;
	import newx.com.control.IScrollPanel;
	import newx.utils.DrawAPI;
	
	public class NxScrollPanel extends NxUIComponent implements IScrollPanel
	{
		private var _view				:	Sprite;
		
		private var _viewHeight		:	int;
		
		private var _viewWidth			:	int;
		
		private var _vScrollSkin 		:	String;
		
		private var _hScrollSkin		:	String;
		
		private var _vScrollBar		:	NxScrollBar;
		
		private var _hScrollBar		:	NxScrollBar;
		
		private var _hscrollMinSize	:	Number		=	15;	
		
		private var _vscrollMinSize	:	Number		=	15;;
		
		private var _verticalScrollPolicy	: String	=	ScrollPolicy.AUTO;
		
		private var _horizontalScrollPolicy: String	=	ScrollPolicy.AUTO;
		
		private var _hscrollDistance	:	int			=	15;
		
		private var _vscrollDistance	:	int			=	15;	
		
		private var _width				:	int			=	300;
		
		private var _height			:	int			=	300;
		
		private var _autoScroll		: 	Boolean		=	true;
		
		
		public function NxScrollPanel()
		{
			_view = new Sprite();
			addChild(_view);
			_view.addEventListener(Event.RESIZE, onResize);
		}
		
		public function get viewWidth():uint
		{
			return _viewWidth;
		}
		
		public function set viewWidth(val:uint):void
		{
			_viewWidth = val;
			
			updataScrollArea();
		}
		
		public function get viewHeight():uint
		{
			return _viewHeight;
		}
		
		public function set viewHeight(val:uint):void
		{
			_viewHeight = val;
			
			updataScrollArea();
		}
		
		/**
		 * 滚动面板宽度
		 */
		override public function get width():Number
		{
			return _width;
		}
		
		override public function set width(value:Number):void
		{
			_width = value;
			
			updataScrollArea();
		}
		
		/**
		 * 滚动面板高度
		 */
		override public function get height():Number
		{
			return _height;
		}
		
		override public function set height(value:Number):void
		{
			_height = value;
			
			updataScrollArea();
		}
		
		public function get vScrollSkin():String
		{
			return _vScrollSkin;
		}
		
		public function set vScrollSkin(val:String):void
		{
			if(val == "" || val == _vScrollSkin) return;
			
			_vScrollSkin = val;
		
			_vScrollBar = new NxScrollBar(NxScrollBar.VERTICAL, 0, _viewWidth, 
				10, new NxScrollSkin(_vScrollSkin));
		
			addScrollBar();
		}
		
		public function get hScrollSkin():String
		{
			return _hScrollSkin;
		}
		
		public function set hScrollSkin(val:String):void
		{
			if(val == "" || val == _hScrollSkin) return;
			
			_hScrollSkin = val;
			
			_hScrollBar = new NxScrollBar(NxScrollBar.HORIZONTAL, 0, _viewHeight,
				10, new NxScrollSkin("", _hScrollSkin));
			
			addScrollBar();
		}
		
		public function addView(val:DisplayObject):void
		{
			if(val == null) return;
			
			_view.addChild(val);
//			viewHeight = _view.height;
//			viewWidth = _view.width;
			viewWidth = DrawAPI.getTrueRect(_view).width;
			viewHeight = DrawAPI.getTrueRect(_view).height;
			
			addScrollBar();
			updataScrollBar();
			
		//	_view.addEventListener(MouseEvent.MOUSE_WHEEL, onMouseWheel);
			addEventListener(MouseEvent.MOUSE_WHEEL, onMouseWheel);
		}
		/**
		 * 刷新 
		 */		
		public function upDataScrollPanel(type:int):void
		{
			if(type == 0)
			{
				viewWidth = DrawAPI.getTrueRect(_view).width;
			}
			else if(type == 1)
			{
				viewHeight = DrawAPI.getTrueRect(_view).height;
			}
			else if(type == 2)
			{
				viewWidth = DrawAPI.getTrueRect(_view).width;
				viewHeight = DrawAPI.getTrueRect(_view).height;
			}

			
			addScrollBar();
			updataScrollBar();
		}
		
		public function removeView(val:DisplayObject):void
		{
			if(val.parent == _view)
			{
				_view.removeChild(val)
			}
				
			viewWidth = DrawAPI.getTrueRect(_view).width;
			viewHeight = DrawAPI.getTrueRect(_view).height;
			updataScrollBar();
			
			if(_view.numChildren == 0)
			{
				removeScrollBar();
				
				//_view.removeEventListener(MouseEvent.MOUSE_WHEEL, onMouseWheel);
				removeEventListener(MouseEvent.MOUSE_WHEEL, onMouseWheel);
			}
		}
		
		private function onResize(e : Event) : void
		{
			viewWidth = DrawAPI.getTrueRect(_view).width;
			viewHeight = DrawAPI.getTrueRect(_view).height;
			
			updataScrollArea();
		}
		
		public function get hscrollMinSize():Number
		{
			return _hscrollMinSize;
		}
		
		public function set hscrollMinSize(val:Number):void
		{
			_hscrollMinSize = val;
			
			_hScrollBar.minSize = _vscrollMinSize;
		}
		
		public function get vscrollMinSize():Number
		{
			return _vscrollMinSize;
		}
		
		public function set vscrollMinSize(val:Number):void
		{
			_vscrollMinSize = val;
			
			_vScrollBar.minSize = _vscrollMinSize;
		}
		
		public function get hscroll():Number
		{
			return _hScrollBar.scroll;
		}
		
		public function set hscroll(val:Number):void
		{
			if(_hScrollBar.parent == null || !_hScrollBar.visible && val != 0) return;
			_hScrollBar.scroll = val;
		}
		
		public function get vscroll():Number
		{
			return _vScrollBar.scroll;
		}
		
		public function set vscroll(val:Number):void
		{
			if(_vScrollBar.parent == null || !_vScrollBar.visible && val != 0) return;
			_vScrollBar.scroll = val;
		}
		
		public function get hscrollDistance():uint
		{
			return _hscrollDistance;
		}
		
		public function set hscrollDistance(val:uint):void
		{
			_hscrollDistance = val;
			
			if(_hscrollDistance < _hscrollMinSize)
			{
				_hscrollDistance = _hscrollMinSize;
			}
			
			_hScrollBar.scrollDistance = _hscrollDistance;
		}
		
		public function get vscrollDistance():uint
		{
			return _vscrollDistance;
		}
		
		public function set vscrollDistance(val:uint):void
		{
			_vscrollDistance = val;
			
			if(_vscrollDistance < _vscrollMinSize)
			{
				_vscrollDistance = _vscrollMinSize;
			}
			
			_vScrollBar.scrollDistance = _vscrollDistance;
		}
		
		public function get verticalScrollPolicy():String
		{
			return _verticalScrollPolicy;
		}
		
		public function set verticalScrollPolicy(val:String):void
		{
			_verticalScrollPolicy = val;
			
			updataScrollBar();
		}
		
		public function get horizontalScrollPolicy():String
		{
			return _horizontalScrollPolicy;
		}
		
		public function set horizontalScrollPolicy(val:String):void
		{
			_horizontalScrollPolicy = val;
			
			updataScrollBar();
		}
		
		/**
		 *	添加滚动条 
		 * 
		 */
		private function addScrollBar() : void
		{
			removeScrollBar();
			if(_view == null) 
			{
				return;
			}
			if(_vScrollBar != null)
			{
				addChild(_vScrollBar);
				if(!_vScrollBar.hasEventListener(NxScrollEvent.UPDATE_AREA))
				{
					_vScrollBar.addEventListener(NxScrollEvent.UPDATE_AREA, onCheckBar);
				}
			}
			if(_hScrollBar != null)
			{
				addChild(_hScrollBar);
				if(!_hScrollBar.hasEventListener(NxScrollEvent.UPDATE_AREA))
				{
					_hScrollBar.addEventListener(NxScrollEvent.UPDATE_AREA, onCheckBar);
				}
			}
			
			updataScrollArea();
			onCheckBar();
		}
		
		/**
		 *	删除滚动条 
		 */
		private function removeScrollBar() : void
		{
			if(_vScrollBar != null)
			{
				if(_vScrollBar.parent != null)
				{
					_vScrollBar.parent.removeChild(_vScrollBar);
				}
				if(_vScrollBar.hasEventListener(NxScrollEvent.UPDATE_AREA))
				{
					_vScrollBar.removeEventListener(NxScrollEvent.UPDATE_AREA, onCheckBar);
				}
			}
			if(_hScrollBar != null)
			{
				if(_hScrollBar.parent != null)
				{
					_hScrollBar.parent.removeChild(_hScrollBar);
				}
				if(_hScrollBar.hasEventListener(NxScrollEvent.UPDATE_AREA))
				{
					_hScrollBar.removeEventListener(NxScrollEvent.UPDATE_AREA, onCheckBar);
				}
			}
		}
		
		/**
		 *	更新显示区域 
		 * 
		 */
		private function updataScrollArea() : void
		{
			if(_viewHeight == 0 || _viewWidth == 0) return;
			var vScroll : Number = 0;
			if(_vScrollBar != null)
			{
				vScroll = _vScrollBar.sliderPerY;
				_vScrollBar.update(_height, (_height - 46) 
					/ _viewHeight * _height);
				
				_vScrollBar.scrollDistance = _vScrollBar.slideableScope * 
									(_vscrollDistance / (_viewHeight - _height));
			}
			
			var hScroll : Number = 0;
			if(_hScrollBar != null)
			{
				hScroll = _hScrollBar.sliderPerX;
				_hScrollBar.update(_width, (_width - 46)
					/ _viewWidth * _width);
				
				_hScrollBar.scrollDistance = _hScrollBar.slideableScope * 
									(_hscrollDistance / (_viewWidth - _height));

			}
			
			updataScrollBar();
		
			//还原原来的进度
			if(!_autoScroll)
			{
				_vScrollBar.scroll = vScroll;
				_hScrollBar.scroll = hScroll;
		
			}
			else//到达最下边
			{
				if(_vScrollBar != null && _vScrollBar.parent != null)
				{
					if(_vScrollBar.visible)
					{
						_vScrollBar.scroll = 1;
					}else
					{
						_vScrollBar.scroll = 0;
					}
					
				}else if(_vScrollBar !=null  && _vScrollBar.parent == null)
				{
					_vScrollBar.scroll = 0;
				}
				
				if(_hScrollBar != null &&_hScrollBar.parent != null)
				{
					if( _hScrollBar.visible)
					{
						_hScrollBar.scroll = 1;
					}else
					{
						_hScrollBar.scroll = 0;
					}
					
				}else if(_hScrollBar !=null  && _hScrollBar.parent == null)
				{
					_hScrollBar.scroll = 0;
				}
			}
		}
			
		
		/**
		 *	维护滑块显示，和位置 
		 * 
		 */
		private function updataScrollBar() : void
		{
			if(_vScrollBar != null && _vScrollBar.parent != null)
			{
				_vScrollBar.visible = true;
				if(_verticalScrollPolicy == ScrollPolicy.AUTO)
				{
					_vScrollBar.visible = _viewHeight > _height;
				}else if(_verticalScrollPolicy == ScrollPolicy.OFF)
				{
					_vScrollBar.parent.removeChild(_vScrollBar);
				}
				if(_vScrollBar.visible)
				{
					_vScrollBar.x = _viewWidth;
				}
			}
				
			if(_hScrollBar != null && _hScrollBar.parent != null)
			{
				_hScrollBar.visible = true;
				if(_horizontalScrollPolicy == ScrollPolicy.AUTO)
				{
					_hScrollBar.visible = _viewWidth > _width;
				}else if(_horizontalScrollPolicy == ScrollPolicy.OFF)
				{
					_hScrollBar.parent.removeChild(_hScrollBar);
				}
				if(_hScrollBar.visible)
				{
					_hScrollBar.y = _viewHeight;
				} 
			}
		}
		
		/**
		 *	点击滑动条 
		 * @param evt
		 * 
		 */
		private function onCheckBar(evt : NxScrollEvent = null) : void
		{
			if(_hScrollBar == null && _vScrollBar == null) return;
			
			addEventListener(Event.ENTER_FRAME, updataCheckBar);
		}
		
		private function updataCheckBar(evt : Event) : void
		{
			removeEventListener(Event.ENTER_FRAME, updataCheckBar);
			
			var _xPoint : int = 0;
			var _yPoint : int = 0;
			
			if(_hScrollBar != null)
			{
				var tcX : Number =	_hScrollBar.barBtn.x - 16;
				_xPoint = (_viewWidth - _width )* tcX / (_width - _hScrollBar.barSize - 32);
			}
			
			if(_vScrollBar != null)
			{
				var tcY : Number =	_vScrollBar.barBtn.y - 16;
				_yPoint = (_viewHeight - _height) * tcY / (_height - _vScrollBar.barSize - 32)
			}
			
			_view.scrollRect = new Rectangle(_xPoint, _yPoint, _width, _height);
		}
		
		private function onMouseWheel(e:MouseEvent):void
		{
			if(_vScrollBar.visible && _vScrollBar.parent != null)
			{
				_vScrollBar.onMouseWheel(e);
				
			}else if(_hScrollBar.visible && _hScrollBar.parent != null)
			{
				_hScrollBar.onMouseWheel(e);
			}
		}

		public function get autoScroll():Boolean
		{
			return _autoScroll;
		}

		public function set autoScroll(value:Boolean):void
		{
			_autoScroll = value;
		}
	}
}